package com.wp;

/**
 * @author 王萍
 * @date 2018/1/2 0002
 */
public class 找素数 {
    public static void calculateNumber(int max) {
        boolean[] isPrime = new boolean[max + 1];
        //奇数为可能为素数
        for (int i = 3; i <= max; i+=2)
            isPrime[i]=true;
        isPrime[2] = true;
        for (int i = 3; i <= Math.sqrt(max); i+=2)
                for (int j = i * i; j <= max; j+=2*i)
                    isPrime[j]=false;
        int count = 0;
        for (int i =1; i <= max; ++i)
            if (isPrime[i]) {
                count++;
                System.out.print(i + " ");
            }
        System.out.println();
        System.out.println(count);
    }

    public static void main(String[] args) {
        calculateNumber(1000);
        System.out.print(2+" ");
        for (int i = 3; i <= 1000; i+=2)
            if (isPrim(i))
                System.out.print(i+" ");
    }

    public static boolean isPrim(int num){
        for (int i = 2; i <= Math.sqrt(num); ++i){
            if(num % i == 0)
                return false;
        }
        return true;
    }

}
